package code;

public class MoreThanHalfNum_Solution {
    public int Solution(int [] array) {
        if(array.length<1){
            return 0;
        }
        int num=array[0],count = 1;
        for(int i=1;i<array.length;i++){
            if(array[i]==num){
                count++;
            }else {
                count--;
                if(count==0){
                    num = array[i];
                    count = 1;
                }
            }
        }
        count = 0;
        for(int i = 0;i<array.length;i++){
            if(array[i] == num){
                count++;
            }
        }
        if(count>array.length/2.0){
            return num;
        }
        return 0;

    }

    public static void main(String[] argv){
        MoreThanHalfNum_Solution moreThanHalfNum_solution = new MoreThanHalfNum_Solution();
        int[] a = new int[]{1,2,3,2,2,2,5,4,2};
        System.out.println(moreThanHalfNum_solution.Solution(a));
    }
}
